<?
    include "../common.php" ;
    $opt = $_POST['opt'] ;
    $arr = [] ;
    if( $opt == 'addBigType' ){
        $TypeName = $_POST['TypeName'] ;
        $sql = "SELECT * FROM `type` WHERE typeName = '{$TypeName}' AND subtypeid IS null";
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $arr['status'] = 1 ;
            $arr['info'] = '此类型已存在' ;
        }  else {
            $sql1 = "INSERT INTO `type` (typeName) VALUES ('{$TypeName}')";
            $res1 = mysqli_query($link,$sql1);
            if($res1){
                $arr['status'] = 2 ;
                $arr['info'] = '添加成功' ;
            } else {
                $arr['status'] = 0 ;
                $arr['info'] = '添加失败' ;
            }
        }
    }
    if( $opt == 'select1' ){
        $page = $_POST['page'] ; 
        $perPage = $_POST['perPage'] ; 
        $pageSt = ( $page - 1 ) * $perPage ;
        $sql = "SELECT `id`, `typeName`, `subtypeid` FROM `type` WHERE subtypeid IS null LIMIT {$pageSt},{$perPage}" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ;
        }
    }
    if( $opt == 'addSType' ){
        $temperName = $_POST['temperName'] ; 
        $sql = "INSERT INTO `temper`(`temperName`) VALUES ('{$temperName}')" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '添加成功！' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败' ;
        }
    }
    if( $opt == 'updStype' ){
        $typeId = $_POST['typeId'] ;
        $subTypeId = $_POST['subTypeId'] ; 
        $temperName = $_POST['temperName'] ;
        $sql = "UPDATE `temper` SET `temperName`='{$temperName}' WHERE id = {$typeId}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '修改成功' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败成功' ;
        }
    }
    if( $opt == 'BSelType' ){
        $str1 = '' ;
        $newArr1 = [] ;
        $sql1 = "SELECT * FROM `type` WHERE 	subtypeid IS null";
        $res1 = mysqli_query($link,$sql1);
        while( $one1 = mysqli_fetch_assoc($res1) ){
            $newArr1[] = $one1 ; 
        }
        foreach( $newArr1 as $key => $value ){
            $str1 .= "<option value='{$value["id"]}'>{$value['typeName']}</option>" ;
        }
        $arr['data1'] = $str1 ; 
        $str2 = '' ;
        $newArr2 = [] ;
        $sql2 = "SELECT * FROM `temper` WHERE is_Delete = 1";
        $res2 = mysqli_query($link,$sql2);
        while( $one2 = mysqli_fetch_assoc($res2) ){
            $newArr2[] = $one2 ; 
        }
        foreach( $newArr2 as $key => $value ){
            $str2 .= "<option value='{$value["id"]}'>{$value['temperName']}</option>" ;
        }
        $arr['data2'] = $str2 ; 
    }
    if( $opt == 'getSubType1' ){
        $TypeVal = $_POST['TypeVal'] ; 
        $sql = "SELECT * FROM `type` WHERE subtypeid = {$TypeVal} " ;
        $res = mysqli_query($link,$sql);
        $newArr = [] ;
        $str = '' ;
        while( $one = mysqli_fetch_assoc($res) ){
            $newArr[] = $one ;  
        } 
        foreach( $newArr as $key => $value ){
            $str .= "<option value='{$value["id"]}'>{$value['typeName']}</option>" ; 
        }
        $arr['data'] = $str ; 
    }
    if( $opt == 'getSubTypeSel' ){
        $subTypeId = $_POST['subTypeId'] ; 
        $sql = "SELECT * FROM `type` WHERE subtypeid = {$subTypeId}" ;
        $res = mysqli_query($link,$sql);
        $newArr = [] ;
        $str = '' ;
        while( $one = mysqli_fetch_assoc($res) ){
            $newArr[] = $one ; 
        } 
        foreach( $newArr as $key => $value ){
            $str .= "<option value='{$value["id"]}'>{$value['typeName']}</option>" ; 
        }
        $arr['data'] = $str ; 
    }
    echo json_encode($arr);
?>